package com.chy.order_meal.service;

import com.chy.order_meal.dao.ResturantDao;
import com.chy.order_meal.dao.TypeDao;
import com.chy.order_meal.po.Resturant;
import com.chy.order_meal.po.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ResturantService {

    @Autowired
    ResturantDao dao;
    @Autowired
    TypeDao typeDao;


    public List<Resturant> getPageByRecentOrderNum(Integer page,Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return dao.getPageByRecentOrderNum(pageable);
    }

    public  List<Resturant> getRestaurant(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        Page<Resturant> all = dao.findAll(pageable);
        return all.getContent();

    }

    public  List<Resturant> getRestaurantByType(Integer typeId, Integer page, Integer size) {
        Type type = typeDao.findById(typeId).get();
        String categories = type.getCategories();
        if(categories == null){
            return null;
        }
        String[] categoriesArr = categories.split("\\|");
        Pageable pageable = PageRequest.of(page-1,size);
        List<String> sList = new ArrayList<String>();
        Collections.addAll(sList,categoriesArr);
        return  dao.getPageByType(sList,pageable);
    }
}
